package com.afkt.project.ui.adapter;


import com.afkt.project.model.bean.CommodityEvaluateBean;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.DevUtils;
import com.dev.base.multiselect.DevMultiSelectMap;
import com.dev.base.multiselect.IMultiSelectEdit;
import com.dev.base.widget.BaseImageView;
import com.dev.base.widget.BaseTextView;
import com.dev.glide.utils.OhosGlide;
import com.dev.utils.app.ViewUtils;
import com.dev.utils.app.helper.ViewHelper;
import com.dev.utils.common.BigDecimalUtils;
import com.dev.utils.common.CollectionUtils;
import ohos.agp.components.*;
import ohos.app.Context;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class MultiSelectAdapter extends BaseItemProvider implements IMultiSelectEdit<MultiSelectAdapter> {


    private List<CommodityEvaluateBean> data = new ArrayList<>();

    private OnSelectListener selectListener;

    // 多选辅助类
    private final DevMultiSelectMap<Integer, CommodityEvaluateBean> multiSelectMap = new DevMultiSelectMap();

    private Context mContext;

    public MultiSelectAdapter(List<CommodityEvaluateBean> data, Context mContext) {
        this.data = data;
        this.mContext = mContext;
    }

    /**
     * 设置选择事件通知事件
     *
     * @param selectListener {@link OnSelectListener}
     * @return {@link MultiSelectAdapter}
     */
    public MultiSelectAdapter setSelectListener(OnSelectListener selectListener) {
        this.selectListener = selectListener;
        return this;
    }

    /**
     * detail: 选择事件通知事件
     *
     * @author Ttt
     */
    public interface OnSelectListener {

        /**
         * 点击选中切换
         *
         * @param position 对应的索引
         * @param now      新的状态
         */
        void onClickSelect(
                int position,
                boolean now
        );

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder_select viewHolder = null;
        if (component ==null){
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_adapter_item_select,null,false);
            viewHolder = new ViewHolder_select();
            viewHolder.commodityName = (BaseTextView) component.findComponentById(ResourceTable.Id_vid_ams_name_tv);
            viewHolder.commodityPrice = (BaseTextView) component.findComponentById(ResourceTable.Id_vid_ams_price_tv);
            viewHolder.commodityImage = (BaseImageView) component.findComponentById(ResourceTable.Id_vid_ams_pic_igview);
            viewHolder.select = (BaseImageView) component.findComponentById(ResourceTable.Id_vid_ams_igview);
            viewHolder.linear = (DirectionalLayout) component.findComponentById(ResourceTable.Id_vid_ams_linear);
            component.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder_select) component.getTag();
        }

        // ===========
        // = 商品信息 =
        // ===========

        // 商品名
        viewHolder.commodityName.setText(data.get(i).commodityName);
        // 商品价格
        viewHolder.commodityPrice.setText("￥" + BigDecimalUtils.operation(data.get(i).commodityPrice)
                .round(2, BigDecimal.ROUND_HALF_UP));
        // 商品图片
        try {
            OhosGlide.with(DevUtils.getAbilitySlice()).load(data.get(i).commodityPicture).def(ResourceTable.Media_icon).into(viewHolder.commodityImage);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // ===========
        // = 多选处理 =
        // ===========
        ViewHolder_select finalViewHolder = viewHolder;
        ViewHelper.get().setVisibility(isEditState(),viewHolder.select)
                .setSelected(multiSelectMap.isSelectKey(i),viewHolder.select)
                .setOnClicks(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {

                        if (!isEditState()) return;
                        // 反选处理
                        multiSelectMap.toggle(i, data.get(i));
                        // 设置是否选中
                        ViewUtils.setSelected(multiSelectMap.isSelectKey(i), finalViewHolder.select);
                        // 触发回调
                        if (selectListener != null) {
                            selectListener.onClickSelect(i, multiSelectMap.isSelectKey(i));
                        }
                        if (finalViewHolder.select.isSelected()){
                            finalViewHolder.select.setPixelMap(ResourceTable.Media_icon_oval_selected);
                        }else {
                            finalViewHolder.select.setPixelMap(ResourceTable.Media_icon_oval_unselected);
                        }
                    }
                },viewHolder.linear);

        if (viewHolder.select.isSelected()){
            viewHolder.select.setPixelMap(ResourceTable.Media_icon_oval_selected);
        }else {
            viewHolder.select.setPixelMap(ResourceTable.Media_icon_oval_unselected);
        }

        return component;
    }

    // 判断是否编辑
    private boolean isEdit;

    @Override
    public boolean isEditState() {
        return isEdit;
    }

    @Override
    public MultiSelectAdapter setEditState(boolean isEdit) {
        this.isEdit = isEdit;
        // 刷新适配器
        LogUtils.error("TAG","刷新适配器");
        notifyDataChanged();
        return this;
    }

    @Override
    public MultiSelectAdapter toggleEditState() {
        // 切换选择状态
        return setEditState(!isEdit);
    }

    @Override
    public MultiSelectAdapter selectAll() {
        LinkedHashMap<Integer, CommodityEvaluateBean> linkedHashMap = new LinkedHashMap<>();
        for (int i = 0, len = getData().size(); i < len; i++) {
            linkedHashMap.put(i, getData().get(i));
        }
        multiSelectMap.putSelects(linkedHashMap);
        // 刷新适配器
        notifyDataChanged();
        return this;
    }

    @Override
    public MultiSelectAdapter clearSelectAll() {
        // 清空选中
        multiSelectMap.clearSelects();
        // 刷新适配器
        notifyDataChanged();
        return this;
    }

    @Override
    public MultiSelectAdapter inverseSelect() {
        // 获取目前选中的数据
        List<Integer> listKeys = multiSelectMap.getSelectKeys();

        // 全选数据
        LinkedHashMap<Integer, CommodityEvaluateBean> linkedHashMap = new LinkedHashMap<>();
        for (int i = 0, len = getData().size(); i < len; i++) {
            linkedHashMap.put(i, getData().get(i));
        }
        multiSelectMap.putSelects(linkedHashMap);

        // 反选处理
        if (!listKeys.isEmpty()) {
            for (Integer key : listKeys) {
                multiSelectMap.unselect(key);
            }
        }
        // 刷新适配器
        notifyDataChanged();
        return this;
    }

    @Override
    public boolean isSelectAll() {
        int size = multiSelectMap.getSelectSize();
        if (size == 0) return false;
        // 判断数量是否一致
        return (CollectionUtils.length(getData()) == multiSelectMap.getSelectSize());
    }

    @Override
    public boolean isSelect() {
        return multiSelectMap.isSelect();
    }


    @Override
    public boolean isNotSelect() {
        return !multiSelectMap.isSelect();
    }

    @Override
    public int getSelectSize() {
        return multiSelectMap.getSelectSize();
    }

    @Override
    public int getDataCount() {
        return getCount();
    }


    public List<CommodityEvaluateBean> getData() {
        return data;
    }

    public void setData(List<CommodityEvaluateBean> data) {
        this.data = data;
    }

    static class ViewHolder_select {
        BaseImageView commodityImage;
        BaseTextView commodityName;
        BaseTextView commodityPrice;
        BaseImageView select;
        DirectionalLayout linear;
    }


}
